import 'package:flutter/material.dart';
import 'package:get/utils.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';

enum ButtonType { primary, normal }

enum ContentType { text, other, cant } // other文案渐变色

class ButtonWidget extends StatelessWidget {
  final String label; // 按钮展示文案
  final String? imageBg; // 按钮的背景图片
  final bool? enable; // 背景颜色是否模糊
  final double? height;
  final double? width;
  final double? fontSize;
  final VoidCallback? onPressed;
  final ContentType? contentType; // 文案展示类型
  final Color? fontColor;
  final Color? backgroundColor;
  final String? fontFamily;
  final AlignmentGeometry alignment;
  final EdgeInsets? paddingText; // 按钮底部背景图不居中情况 需要传padding使文案居中
  final Color colorStart; // ContentType.other类型是 文案渐变色值
  final Color colorEnd; // ContentType.other类型是 文案渐变色值
  final Color? bgColorStart; // ContentType.other类型是 背景渐变色值
  final Color? bgColorEnd; // ContentType.other类型是 背景渐变色值
  final Border? border;
  final FontWeight? fontWeight;
  final int? borderRadius;

  const ButtonWidget({
    super.key,
    required this.label,
    this.enable = true,
    this.onPressed,
    this.height = 48,
    this.width = 322,
    this.contentType = ContentType.text,
    this.fontSize = 16,
    this.fontColor = AppColors.fontColorBlack12,
    this.backgroundColor,
    this.imageBg,
    this.alignment = Alignment.center,
    this.paddingText,
    this.fontFamily = '',
    this.colorStart = AppColors.fontColorGreenStart,
    this.colorEnd = AppColors.fontColorGreenEnd,
    this.bgColorStart = AppColors.startColor,
    this.bgColorEnd = AppColors.endColor,
    this.border,
    this.fontWeight,
    this.borderRadius = 48,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      splashFactory: NoSplash.splashFactory, // 使用 NoSplash 完全禁用水波效果
      onTap: enable! ? onPressed : null,
      splashColor: Colors.transparent, // 点击时的涟漪颜色，设置为透明
      highlightColor: Colors.transparent, // 高亮颜色，设置为透明
      child: Opacity(
        opacity: enable == true ? 1 : 0.6,
        child: Center(
          child: Stack(
            children: [
              imageBg != null
                  ? ImageAssetWidget(
                    imageUrl: imageBg!,
                    width: width,
                    height: height,
                    fit: BoxFit.cover,
                    alignment: alignment,
                  )
                  : SizedBox(),
              Container(
                height: height,
                width: width,
                // constraints: BoxConstraints(
                //   maxWidth: 200
                // ),
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [bgColorStart!, bgColorEnd!], // 定义渐变颜色
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                  ),
                  // color: AppColors.backgroundColorBlack,
                  borderRadius: BorderRadius.circular(borderRadius!.px),
                  border: border,
                  // type == ButtonType.normal
                  //     ? Border.all(color: AppColors.borderGreen, width: 1)
                  //     : null,
                  // gradient:
                  //     imageBg == null
                  //         ? const LinearGradient(
                  //           begin: Alignment.centerLeft,
                  //           end: Alignment.centerRight,
                  //           colors: [
                  //             AppColors.backgroundColor,
                  //             AppColors.backgroundColor,
                  //           ],
                  //         )
                  //         : null,
                  color: backgroundColor,
                  // type == ButtonType.normal
                  //     ? (backgroundColor ?? AppColors.backgroundColor)
                  //     : null,
                ),
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: Center(
                    child:
                        contentType == ContentType.text
                            ? Transform.translate(
                              // offset: Offset(0, 1), // 下移1像素
                              offset: Offset(0, 0), // 下移1像素
                              child: Text(
                                label,
                                style: TextStyle(
                                  color: fontColor,
                                  fontSize: fontSize,
                                  fontWeight: fontWeight,
                                  // overflow: TextOverflow.ellipsis
                                ),
                              ),
                            )
                            : _otherType(),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  _otherType() {
    return Container(
      height: height,
      width: width,
      alignment: Alignment.center,
      child: ShaderMask(
        shaderCallback: (rect) {
          return LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [colorStart, colorEnd],
          ).createShader(rect);
        },
        child: Container(
          padding: paddingText,
          child: Transform.translate(
            offset: Offset(0, 1), // 下移1像素
            child: Text(
              textAlign: TextAlign.center,
              label,
              style: TextStyle(
                height: 1.2,
                color: AppColors.fontColor,
                fontSize: fontSize,
                fontFamily: 'CustomFont',
              ),
            ),
          ),
        ),
      ),
    );
  }
}
